/**
 * MenuFrameChooseRace.java
 *
 * Roslan's Remake of MOO in 3D using Ardor3D
 * (C) 2011 Roslan Amir. All rights reserved.
 */

package com.roslan.games.moo3d.ui.menu;

import com.ardor3d.extension.ui.UIButton;
import com.ardor3d.extension.ui.UILabel;
import com.ardor3d.extension.ui.UIPanel;
import com.ardor3d.extension.ui.UIRadioButton;
import com.ardor3d.extension.ui.event.ActionEvent;
import com.ardor3d.extension.ui.event.ActionListener;
import com.ardor3d.extension.ui.layout.GridLayout;
import com.ardor3d.extension.ui.layout.GridLayoutData;
import com.ardor3d.extension.ui.layout.RowLayout;
import com.ardor3d.extension.ui.util.Alignment;
import com.ardor3d.extension.ui.util.ButtonGroup;
import com.roslan.games.moo3d.GameData.Actions;
import com.roslan.games.moo3d.data.Race;

/**
 * Comments go here.
 * 
 * @author Roslan Amir
 * @version 1.0 - Jan 27, 2011
 */
public class MenuFrameChooseRace extends MenuFrameBase {

	/**
	 * Default no-arg constructor.
	 */
	public MenuFrameChooseRace() {
		super("Choose Your Race");

		/*
		 * Panel with a Grid layout
		 */
		final UIPanel grid = new UIPanel();
		grid.setLayout(new GridLayout());
		grid.setMargin(FRAME_MARGINS);
		grid.setBorder(SOLID_BORDER);
		// grid.setMargin(HudManager.MARGINS_ZERO);
		// grid.setPadding(HudManager.PADDING_2000);
		// grid.setBorder(HudManager.SOLID_BORDER_TWO);
		vpanel.add(grid);

		/*
		 * Race Names
		 */
		final ButtonGroup chooseRaceGroup = new ButtonGroup();
		final UIRadioButton chooseRaceHuman = new UIRadioButton("[b]" + Race.getRaceNamePlural(0) + "[/b]");
		chooseRaceHuman.setStyledText(true);
		chooseRaceHuman.setSelectable(true);
		chooseRaceHuman.setSelected(gameData.playerRaceIndex == 0);
		chooseRaceHuman.setGroup(chooseRaceGroup);
		grid.add(chooseRaceHuman);

		final UIRadioButton chooseRaceMrrshan = new UIRadioButton("[b]" + Race.getRaceNamePlural(1) + "[/b]");
		chooseRaceMrrshan.setStyledText(true);
		chooseRaceMrrshan.setSelectable(true);
		chooseRaceMrrshan.setSelected(gameData.playerRaceIndex == 1);
		chooseRaceMrrshan.setLayoutData(GridLayoutData.WrapAndGrow);
		chooseRaceMrrshan.setGroup(chooseRaceGroup);
		grid.add(chooseRaceMrrshan);

		final UIRadioButton chooseRaceSilicoid = new UIRadioButton("[b]" + Race.getRaceNamePlural(2) + "[/b]");
		chooseRaceSilicoid.setStyledText(true);
		chooseRaceSilicoid.setSelectable(true);
		chooseRaceSilicoid.setSelected(gameData.playerRaceIndex == 2);
		chooseRaceSilicoid.setGroup(chooseRaceGroup);
		grid.add(chooseRaceSilicoid);

		final UIRadioButton chooseRaceSakkra = new UIRadioButton("[b]" + Race.getRaceNamePlural(3) + "[/b]");
		chooseRaceSakkra.setStyledText(true);
		chooseRaceSakkra.setSelectable(true);
		chooseRaceSakkra.setSelected(gameData.playerRaceIndex == 3);
		chooseRaceSakkra.setLayoutData(GridLayoutData.WrapAndGrow);
		chooseRaceSakkra.setGroup(chooseRaceGroup);
		grid.add(chooseRaceSakkra);

		final UIRadioButton chooseRacePsilons = new UIRadioButton("[b]" + Race.getRaceNamePlural(4) + "[/b]");
		chooseRacePsilons.setStyledText(true);
		chooseRacePsilons.setSelectable(true);
		chooseRacePsilons.setSelected(gameData.playerRaceIndex == 4);
		chooseRacePsilons.setGroup(chooseRaceGroup);
		grid.add(chooseRacePsilons);

		final UIRadioButton chooseRaceAlkari = new UIRadioButton("[b]" + Race.getRaceNamePlural(5) + "[/b]");
		chooseRaceAlkari.setStyledText(true);
		chooseRaceAlkari.setSelectable(true);
		chooseRaceAlkari.setSelected(gameData.playerRaceIndex == 5);
		chooseRaceAlkari.setLayoutData(GridLayoutData.WrapAndGrow);
		chooseRaceAlkari.setGroup(chooseRaceGroup);
		grid.add(chooseRaceAlkari);

		final UIRadioButton chooseRaceKlackon = new UIRadioButton("[b]" + Race.getRaceNamePlural(6) + "[/b]");
		chooseRaceKlackon.setStyledText(true);
		chooseRaceKlackon.setSelectable(true);
		chooseRaceKlackon.setSelected(gameData.playerRaceIndex == 6);
		chooseRaceKlackon.setGroup(chooseRaceGroup);
		grid.add(chooseRaceKlackon);

		final UIRadioButton chooseRaceBulrathi = new UIRadioButton("[b]" + Race.getRaceNamePlural(7) + "[/b]");
		chooseRaceBulrathi.setStyledText(true);
		chooseRaceBulrathi.setSelectable(true);
		chooseRaceBulrathi.setSelected(gameData.playerRaceIndex == 7);
		chooseRaceBulrathi.setLayoutData(GridLayoutData.WrapAndGrow);
		chooseRaceBulrathi.setGroup(chooseRaceGroup);
		grid.add(chooseRaceBulrathi);

		final UIRadioButton chooseRaceMeklar = new UIRadioButton("[b]" + Race.getRaceNamePlural(8) + "[/b]");
		chooseRaceMeklar.setStyledText(true);
		chooseRaceMeklar.setSelectable(true);
		chooseRaceMeklar.setSelected(gameData.playerRaceIndex == 8);
		chooseRaceMeklar.setGroup(chooseRaceGroup);
		grid.add(chooseRaceMeklar);

		final UIRadioButton chooseRaceDarlok = new UIRadioButton("[b]" + Race.getRaceNamePlural(9) + "[/b]");
		chooseRaceDarlok.setStyledText(true);
		chooseRaceDarlok.setSelectable(true);
		chooseRaceDarlok.setSelected(gameData.playerRaceIndex == 9);
		chooseRaceDarlok.setLayoutData(GridLayoutData.WrapAndGrow);
		chooseRaceDarlok.setGroup(chooseRaceGroup);
		grid.add(chooseRaceDarlok);

		// /*
		// * Panel with a horizontal layout
		// */
		// final UIPanel hpanel1 = new UIPanel();
		// hpanel1.setLayout(new RowLayout(true));
		// hpanel1.setBorder(SOLID_BORDER_TWO);
		// vpanel.add(hpanel1);

		/*
		 * Race Image
		 */
		final UILabel raceHeadImageLabel = new UILabel("");
		raceHeadImageLabel.setMargin(FRAME_MARGINS);
		raceHeadImageLabel.setBorder(SOLID_BORDER);
		raceHeadImageLabel.setAlignment(Alignment.MIDDLE);
		raceHeadImageLabel.setLayoutData(new GridLayoutData(1, false, true));
		raceHeadImageLabel.setIcon(SUBTEX_RACE_HEADS[gameData.playerRaceIndex]);
		raceHeadImageLabel.updateIconDimensionsFromIcon();
		vpanel.add(raceHeadImageLabel);

		/*
		 * Race Description
		 */
		final UILabel raceDescImageLabel = new UILabel("");
		raceDescImageLabel.setMargin(FRAME_MARGINS);
		raceDescImageLabel.setBorder(SOLID_BORDER);
		raceDescImageLabel.setAlignment(Alignment.MIDDLE);
		raceDescImageLabel.setLayoutData(new GridLayoutData(3, true, true));
		raceDescImageLabel.setIcon(SUBTEX_RACE_DESCS[gameData.playerRaceIndex]);
		raceDescImageLabel.updateIconDimensionsFromIcon();
		vpanel.add(raceDescImageLabel);

		/*
		 * Add listeners to each radio button
		 */
		chooseRaceHuman.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.playerRaceIndex = 0;
				setRaceHeadAndDescription(raceHeadImageLabel, raceDescImageLabel);
			}
		});
		chooseRaceMrrshan.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.playerRaceIndex = 1;
				setRaceHeadAndDescription(raceHeadImageLabel, raceDescImageLabel);
			}
		});
		chooseRaceSilicoid.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.playerRaceIndex = 2;
				setRaceHeadAndDescription(raceHeadImageLabel, raceDescImageLabel);
			}
		});
		chooseRaceSakkra.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.playerRaceIndex = 3;
				setRaceHeadAndDescription(raceHeadImageLabel, raceDescImageLabel);
			}
		});
		chooseRacePsilons.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.playerRaceIndex = 4;
				setRaceHeadAndDescription(raceHeadImageLabel, raceDescImageLabel);
			}
		});
		chooseRaceAlkari.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.playerRaceIndex = 5;
				setRaceHeadAndDescription(raceHeadImageLabel, raceDescImageLabel);
			}
		});
		chooseRaceKlackon.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.playerRaceIndex = 6;
				setRaceHeadAndDescription(raceHeadImageLabel, raceDescImageLabel);
			}
		});
		chooseRaceBulrathi.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.playerRaceIndex = 7;
				setRaceHeadAndDescription(raceHeadImageLabel, raceDescImageLabel);
			}
		});
		chooseRaceMeklar.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.playerRaceIndex = 8;
				setRaceHeadAndDescription(raceHeadImageLabel, raceDescImageLabel);
			}
		});
		chooseRaceDarlok.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.playerRaceIndex = 9;
				setRaceHeadAndDescription(raceHeadImageLabel, raceDescImageLabel);
			}
		});

		/*
		 * Panel with a horizontal layout
		 */
		final UIPanel hpanel2 = new UIPanel();
		hpanel2.setLayout(new RowLayout(true));
		hpanel2.setBorder(SOLID_BORDER);
		vpanel.add(hpanel2);

		/*
		 * Back button
		 */
		final UIButton backButton = new UIButton("[b]Back[/b]");
		backButton.setStyledText(true);
		backButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.action = Actions.SHOW_NEW_GAME_MENU;
			}
		});
		hpanel2.add(backButton);

		/*
		 * Continue button
		 */
		final UIButton continueButton = new UIButton("[b]Continue[/b]");
		continueButton.setStyledText(true);
		continueButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.action = Actions.SHOW_CHOOSE_BANNER_MENU;
			}
		});
		hpanel2.add(continueButton);

		/*
		 * Finalization
		 */
		this.setName("Choose Your Race");
		this.setContentPanel(vpanel);
		super.setUpFrame();
	}

	/**
	 * Comment for method.
	 * 
	 * @param raceHeadImageLabel
	 * @param raceDescImageLabel
	 */
	private void setRaceHeadAndDescription(final UILabel raceHeadImageLabel, final UILabel raceDescImageLabel) {
		raceHeadImageLabel.setIcon(SUBTEX_RACE_HEADS[gameData.playerRaceIndex]);
		raceDescImageLabel.setIcon(SUBTEX_RACE_DESCS[gameData.playerRaceIndex]);
	}

	/**
	 * Update the contents of this frame before showing it.
	 */
	public void update() {
		UIPanel vpanel = this.getContentPanel();

		UIPanel grid = (UIPanel) vpanel.getChild(0);
		UIRadioButton chooseRaceHuman = (UIRadioButton) grid.getChild(0);
		chooseRaceHuman.setSelected(gameData.playerRaceIndex == 0);
		UIRadioButton chooseRaceMrrshan = (UIRadioButton) grid.getChild(1);
		chooseRaceMrrshan.setSelected(gameData.playerRaceIndex == 1);
		UIRadioButton chooseRaceSilicoid = (UIRadioButton) grid.getChild(2);
		chooseRaceSilicoid.setSelected(gameData.playerRaceIndex == 2);
		UIRadioButton chooseRaceSakkra = (UIRadioButton) grid.getChild(3);
		chooseRaceSakkra.setSelected(gameData.playerRaceIndex == 3);
		UIRadioButton chooseRacePsilons = (UIRadioButton) grid.getChild(4);
		chooseRacePsilons.setSelected(gameData.playerRaceIndex == 4);
		UIRadioButton chooseRaceAlkari = (UIRadioButton) grid.getChild(5);
		chooseRaceAlkari.setSelected(gameData.playerRaceIndex == 5);
		UIRadioButton chooseRaceKlackon = (UIRadioButton) grid.getChild(6);
		chooseRaceKlackon.setSelected(gameData.playerRaceIndex == 6);
		UIRadioButton chooseRaceBulrathi = (UIRadioButton) grid.getChild(7);
		chooseRaceBulrathi.setSelected(gameData.playerRaceIndex == 7);
		UIRadioButton chooseRaceMeklar = (UIRadioButton) grid.getChild(8);
		chooseRaceMeklar.setSelected(gameData.playerRaceIndex == 8);
		UIRadioButton chooseRaceDarlok = (UIRadioButton) grid.getChild(9);
		chooseRaceDarlok.setSelected(gameData.playerRaceIndex == 9);

		// UIPanel hpanel = (UIPanel) vpanel.getChild(1);
		// UILabel raceImageLabel = (UILabel) hpanel.getChild(0);
		// UILabel raceDescriptionLabel = (UILabel) hpanel.getChild(1);
		UILabel raceImageLabel = (UILabel) vpanel.getChild(1);
		UILabel raceDescriptionLabel = (UILabel) vpanel.getChild(2);
		setRaceHeadAndDescription(raceImageLabel, raceDescriptionLabel);
	}

}
